/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[TestServletConfig.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package servlet.api;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

/**
 * javax.servlet.ServletConfig测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
@WebServlet(name = "TestServletConfig", urlPatterns = "/servletConfig", initParams = {
		@WebInitParam(name = "username", value = "Sean.Yang"),
		@WebInitParam(name = "password", value = "123456") }, description = "这是测试ServletConfig的Servlet")
public class TestServletConfig extends GenericServlet {

	private static final long serialVersionUID = 6714926682441958835L;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);// 必须调用父类的方法, 才能在service()方法里调用getServletConfig()
		String servletName = config.getServletName();
		System.out.println("这个Servlet的名字是" + servletName);

		Enumeration<String> paramNames = config.getInitParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			String paramValue = config.getInitParameter(paramName);
			System.out.println(paramName + "=" + paramValue);
		}
	}

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// 重写init(ServletConfig config)方法,
		// 如果不显示调用super.init(config), 则getServletConfig()结果为null
		System.out.println("ServletConfig = " + this.getServletConfig());
	}
}
